import electron from 'electron'
import { APP_EXIT } from './singal';
import { Exception } from 'handlebars';

class IPCMain {
    constructor(mainwindow) {
        this.mainwindow = mainwindow
    }

    listen(eventName, func) {
        electron.ipcMain.on(eventName, (event, arg) => {
            func(event, arg, this.mainwindow);
        })
    }
}

class IPCThread {
    listen(eventName, func) {
        electron.ipcRenderer.on(eventName, (event, message) => {
            func(event, message);
        })
    }
}

function initalIpcMain(mainwindow) {
    const ipcmain = new IPCMain(mainwindow)

    ipcmain.listen(APP_EXIT, (event, msg, main) => {
        main.quit()
    })
}

function send(eventName, msg, mode = 'M') {
    if (mode === 'M') {
        electron.ipcMain.send(eventName, msg)
    } else if (mode === 'm') {
        electron.ipcRenderer.send(eventName, msg)
    } else {
        throw Exception(`not support parameter "mode" value ${mode}, just support it like {M|m}`)
    }
}

export { initalIpcMain, send }